const injectApp = function ({ router, app }) {
  if (router.__isInjectApp) return
  router.beforeEach((to, from, next) => {
    if (!app.isInit) {
      next()
    } else {
      app.$emit(app.EventType.pageLeave, {
        to,
        from,
        next
      })
    }
  })
  router.beforeResolve((to, from, next) => {
    if (!app.isInit) {
      if (to.name != '404') {
        app.init({ to, from, next })
      } else {
        next()
      }
    } else {
      app.$emit(app.EventType.beforePageEnter, {
        to,
        from,
        next
      })
    }
  })
  router.afterEach((to, from) => {
    app.$emit(app.EventType.pageEnter, {
      to,
      from
    })
  })
  router.__isInjectApp = true
}
export default injectApp
